package com.neulogistics.contrib.procuredcenter.api.controller;

import com.blackto.lib.mediat.core.IMediator;
import com.neulogistics.contrib.procuredcenter.api.application.command.CreateGoodsListCommand;
import com.neulogistics.contrib.procuredcenter.api.application.command.UpdateGoodsListCommand;
import com.neulogistics.contrib.procuredcenter.api.application.query.IGoodsListQueryService;
import com.neulogistics.contrib.procuredcenter.api.application.query.ITreeQueryService;
import com.neulogistics.contrib.procuredcenter.api.application.viewmodel.ClassifyViewModel;
import com.neulogistics.contrib.procuredcenter.api.application.viewmodel.GoodsListViewModel;
import com.neulogistics.contrib.procuredcenter.api.application.viewmodel.TreeNodeViewModel;
import com.neulogistics.contrib.procuredcenter.domain.model.aggregate.GoodsList;
import com.neulogistics.contrib.procuredcenter.domain.repository.IGoodsListRepository;
import com.neulogistics.infrastructure.common.serviceresult.BaseResponse;
import com.neulogistics.infrastructure.common.serviceresult.ErrorCode;
import com.neulogistics.infrastructure.common.serviceresult.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;
import java.util.List;

@RestController
@Api(tags = "商品一级分类管理")
@RequestMapping("/procuredCenter/goodsList")
public class GoodsListController {
    @Resource
    IMediator mediator;

    @Resource
    IGoodsListQueryService goodsListQueryService;

    @Resource
    ITreeQueryService treeQueryService;

    @ApiOperation("新增一级分类")
    @PostMapping("/create")
    public BaseResponse<String> create(@RequestBody @Validated @NotNull CreateGoodsListCommand command){

        BaseResponse<String> baseResponse = mediator.send(command);

        return baseResponse == null ? ResultUtils.error(ErrorCode.SYSTEM_ERROR) : baseResponse;
    }

    @ApiOperation("修改一级分类")
    @PostMapping("/update")
    public BaseResponse update(@RequestBody @Validated @NotNull UpdateGoodsListCommand command){

        BaseResponse baseResponse = mediator.send(command);

        return baseResponse == null ? ResultUtils.error(ErrorCode.SYSTEM_ERROR) : baseResponse;
    }

    @ApiOperation("根据id获取一级分类")
    @GetMapping("/get/{listId}")
    public BaseResponse<GoodsListViewModel> get(@PathVariable(value = "listId") @NotNull @Positive Integer listId){

        GoodsListViewModel goodsListViewModel = goodsListQueryService.getById(listId);

        return ResultUtils.success(goodsListViewModel);
    }

    @ApiOperation("根据set获取一级分类")
    @GetMapping("/getClassifyBySetId")
    public BaseResponse<ClassifyViewModel> getClassifyBySetId(@NotNull @Positive Integer setId){

        ClassifyViewModel classifyViewModel = goodsListQueryService.getBySetId(setId);

        return ResultUtils.success(classifyViewModel);
    }

    @ApiOperation("获取所有一级分类")
    @GetMapping("/list")
    public BaseResponse<List<GoodsListViewModel>> list(){

        List<GoodsListViewModel> goodsListViewModels = goodsListQueryService.list();

        return ResultUtils.success(goodsListViewModels);
    }

    @ApiOperation("获取分类树")
    @GetMapping("/tree")
    public BaseResponse<List<TreeNodeViewModel>> tree(){

        List<TreeNodeViewModel> tree = treeQueryService.getTree();

        return ResultUtils.success(tree);
    }
}
